package cn.masterj.springcloud.service.fallback;

import cn.masterj.springcloud.service.IPaymentService;
import feign.hystrix.FallbackFactory;
import lombok.Cleanup;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
import java.io.StringWriter;


/**
 * @author masterJ
 * @create 2020-04-11 15:08
 */
@Component
public class PaymentServiceFallbackFactory implements FallbackFactory<IPaymentService> {

    @Override
    public IPaymentService create(Throwable e) {
        e.printStackTrace();
        String errorMsg = "";
        try {
            @Cleanup
            StringWriter sw = new StringWriter();
            @Cleanup
            PrintWriter pw = new PrintWriter(sw);
            // 将出错的栈信息输出到printWriter中
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();
            errorMsg = sw.toString();
        }catch (Exception e1){
            e1.printStackTrace();
        }


        String finalErrorMsg = errorMsg;
        return new IPaymentService() {
            @Override
            public String payment_ok() {
                return null;
            }

            @Override
            public String payment_timeout() {

                return "我是PaymentServiceFallbackFactory----payment_timeout，异常信息是：\r\n" + finalErrorMsg;
            }

            @Override
            public String payment_exception() {
                return null;
            }

            @Override
            public String payment_circuit(Long id) {
                return null;
            }
        };
    }
}
